package com.funwell.modules.controller;

import com.funwell.modules.controller.request.user.UserLoginRequestBean;
import com.funwell.modules.controller.response.user.UserResponseBean;
import com.funwell.modules.model.user.User;
import com.funwell.modules.service.user.UserService;
import com.funwell.server.annotation.ACS;
import com.funwell.server.base.BaseController;
import com.funwell.server.utils.ResponseEntity;
import com.funwell.server.utils.ResponseEntityUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 *
 * @author sunfangwei
 *
 */
@Api(value="用户功能",description="用户管理模块",produces = "application/json")
@RestController
@RequestMapping(value="user")
public class UserController extends BaseController {
	
	@Autowired
	private UserService userService;
	
	@ACS(allowAnonymous = true)
	@ApiOperation(value = "登录", notes = "参数用户名，密码，返回token")
	@RequestMapping(value = "/login", method = RequestMethod.POST)
	public ResponseEntity<UserResponseBean> login(@Valid @RequestBody UserLoginRequestBean bean) {
		UserResponseBean responseBean = new UserResponseBean();
		User user = userService.login(bean);
		// 创建访问token
		String accessToken = super.generateAccessToken(request);
		super.setAccessTokenAttribute(request, accessToken);
		super.sessionUser(request, user);
		BeanUtils.copyProperties(user, responseBean);
		responseBean.setAccessToken(accessToken);
		return ResponseEntityUtil.success(responseBean);
	}
	

}
